package com.xnx3.banmahuishou.wangge.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.BaseVO;
import com.xnx3.banmahuishou.core.entity.Wangge;
import com.xnx3.banmahuishou.core.entity.WanggeSettime;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 系统管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouWanggeWanggeSettimeController")
@RequestMapping("/wangge/wanggesettime/")
public class WanggeSettimeController extends BaseController {
	@Resource
	private SqlService sqlService;

	/**
	 * 营业时间列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"wangge_username"});
		sql.appendWhere("wangge_id = " + getUserId());
		int count = sqlService.count("laji_wangge_settime", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_wangge_settime", page);
		sql.setDefaultOrderBy("laji_wangge_settime.id DESC");
		List<WanggeSettime> list = sqlService.findBySql(sql, WanggeSettime.class);
		
		ActionLogUtil.insert(request, "系统管理", "第"+page.getCurrentPageNumber()+"页");
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "/wangge/wanggesettime/list";
	}
	
	/**
	 * 新增/修改营业时间
	 * @param id 要修改的id 。 如果是新增，这里传入0或者不传
	 */
	@RequestMapping("edit${url.suffix}")
	public String edit(
			@RequestParam(value = "id", required = false, defaultValue="0") int id,
			Model model, HttpServletRequest request){
		WanggeSettime entity;
		
		if(id < 1){
			//新增
			entity = new WanggeSettime();
			ActionLogUtil.insert(request, "进入营业时间新增页面");
		}else{
			//修改
			entity = sqlService.findById(WanggeSettime.class, id);
			if(entity == null){
				return error(model, "要修改的营业时间不存在");
			}
			ActionLogUtil.insert(request, "进入修改营业页面", entity.toString());
		}
		
		//编辑页面
		model.addAttribute("entity", entity);
		return "/wangge/wanggesettime/edit";
	}
	
	/**
	 * 新增、修改的营业时间进行保存
	 */
	@RequestMapping(value="save.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO variableSave(WanggeSettime inputEntity, HttpServletRequest request){
		WanggeSettime entity = null;			
		
		if(inputEntity.getId() == null || inputEntity.getId() < 1){
			//新增
			entity = new WanggeSettime();
			
		}else{
			//修改
			entity = sqlService.findById(WanggeSettime.class, inputEntity.getId());
			if(entity == null){
				return error("要修改的目标不存在");
			}
		}
		User user = sqlService.findById(User.class, getUserId());
		Wangge wangge = sqlService.findById(Wangge.class, getUserId());
		if(inputEntity.getStartTime() == null) {
			return error("选择开始时间");
		}
		if(inputEntity.getEndTime() == null) {
			return error("请选择结束时间");
		}
		if(inputEntity.getRestTime() == null) {
			return error("请选择休息日");
		}
		//保存
		entity.setDailishangId(wangge.getDailishangUserid());
		entity.setDailishangUsername(wangge.getDailishangUsername());
		entity.setWanggeId(user.getId());
		entity.setWanggeUsername(user.getNickname());
		entity.setStartTime(inputEntity.getStartTime());
		entity.setEndTime(inputEntity.getEndTime());
		entity.setRestTime(inputEntity.getRestTime());
		entity.setNewYearStartRestTime(inputEntity.getNewYearStartRestTime());
		entity.setNewYearEndRestTime(inputEntity.getNewYearEndRestTime());
		entity.setNewYearTip(inputEntity.getNewYearTip());
		
		sqlService.save(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "保存营业时间", entity.toString());
		return success();
	}
	
	/**
	 * 删除营业时间
	 */
	@RequestMapping(value="delete.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO delete(@RequestParam(value = "id", required = false, defaultValue="0") int id, HttpServletRequest request){
		WanggeSettime entity = sqlService.findById(WanggeSettime.class, id);
		if(entity == null){
			return error("要删除的目标不存在");
		}
		sqlService.delete(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "删除营业时间", entity.toString());
		return success();
	}
}
